package com.licheedev.someext.recycler

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.licheedev.someext.R


class BindingViewHolder<VB : ViewBinding>(val binding: VB) : RecyclerView.ViewHolder(binding.root) {
    constructor(
        parent: ViewGroup,
        bindingInflate: (inflater: LayoutInflater, root: ViewGroup, attachToRoot: Boolean) -> VB
    ) : this(bindingInflate(LayoutInflater.from(parent.context), parent, false))
}

/**
 * 填充BindingViewHolder
 * @receiver ViewGroup
 * @param bindingInflate Function3<[@kotlin.ParameterName] LayoutInflater, [@kotlin.ParameterName] ViewGroup, [@kotlin.ParameterName] Boolean, VB>
 * @return BindingViewHolder<VB>
 */
fun <VB : ViewBinding> ViewGroup.inflateBindingHolder(
    bindingInflate: (inflater: LayoutInflater, root: ViewGroup, attachToRoot: Boolean) -> VB
): BindingViewHolder<VB> {
    return BindingViewHolder(this, bindingInflate)
}


abstract class PosOnclickListener : View.OnClickListener {
    var curPos: Int? = null
}

abstract class PosOnLongClickListener : View.OnLongClickListener {
    var curPos: Int? = null
}


inline fun View.bindOnclickListener(
    viewHolder: RecyclerView.ViewHolder,
    position: Int? = null,
    crossinline onClick: (currentPos: Int) -> Unit
) {
    val tag = this.getTag(R.id.posOnClickListener)
    val listener = if (tag != null) {
        tag as PosOnclickListener
    } else {
        object : PosOnclickListener() {
            override fun onClick(v: View?) {
                val pos = curPos ?: viewHolder.adapterPosition
                if (pos != RecyclerView.NO_POSITION && viewHolder.adapterPosition != RecyclerView.NO_POSITION) {
                    onClick.invoke(pos)
                }
            }
        }.also {
            this.setOnClickListener(it)
            this.setTag(R.id.posOnClickListener, it)
        }
    }
    listener.curPos = position
}

inline fun View.bindOnLongClickListener(
    viewHolder: RecyclerView.ViewHolder,
    position: Int? = null,
    crossinline onClick: (currentPos: Int) -> Boolean
) {
    val tag = this.getTag(R.id.posOnLongClickListener)
    val listener = if (tag != null) {
        tag as PosOnLongClickListener
    } else {
        object : PosOnLongClickListener() {
            override fun onLongClick(v: View?): Boolean {
                val pos = curPos ?: viewHolder.adapterPosition
                if (pos != RecyclerView.NO_POSITION && viewHolder.adapterPosition != RecyclerView.NO_POSITION) {
                    return onClick.invoke(pos)
                } else {
                    return false
                }
            }
        }.also {
            this.setOnLongClickListener(it)
            this.setTag(R.id.posOnLongClickListener, it)
        }
    }
    listener.curPos = position
}

/**
 * 清除itemView根布局的点击监听
 * @receiver RecyclerView.ViewHolder
 * @param clearClick Boolean
 * @param clearLongClick Boolean
 */
fun RecyclerView.ViewHolder.clearItemViewClickListener(
    clearClick: Boolean = true,
    clearLongClick: Boolean = true
) {

    if (clearClick) {
        itemView.setOnClickListener(null)
        itemView.isClickable = false
    }

    if (clearLongClick) {
        itemView.setOnLongClickListener(null)
        itemView.isLongClickable = false
    }
}


